import akka.actor.Actor
import akka.actor.Props

class Manager extends Actor{
  var count = 0
  
  def receive = {
    case Start =>{
      for (i <- 1 to Main.nOfActors){
        if (Main.algorithm == "gossip")
        {
	        val a = context.system.actorOf(Props[GossipNode], name = i.toString)			
		    a ! SetNeighbours(Main.nOfActors, Main.network)
        }
        else
        {
            val a = context.system.actorOf(Props[PushSumNode], name = i.toString)		
		    a ! SetNeighbours(Main.nOfActors, Main.network)
        }
      }
    }
    case LoadedNeighbours => {
      count += 1
      if (count == Main.nOfActors)
      {
        count = 0
        Main.startTime = System.currentTimeMillis();
        if (Main.algorithm == "gossip"){
            //println("Manager sending rumor")
        	context.system.actorSelection("akka://System/user/1") ! Rumor
        }
        else{
            //println("Manager sending first start msg")
        	context.system.actorSelection("akka://System/user/1") ! Start
        }
      }
    }
    case Dead =>{
      count += 1
      //println("dead count" + count)
      if (count >= Main.nOfActors)
      {
        context.system.shutdown()
      }
    }
  }  
}